mask_siteid_sampling <- site_protocol_quanti[
  site_protocol_quanti$variable == "year" &
    site_protocol_quanti$n >= 10,
  ]$siteid

mask_siteid_protocol <- site_protocol_quali[
  site_protocol_quali$unitabundance %in% c("Count", "Ind.100m2"), ]$siteid

mask_siteid <- mask_siteid_sampling[mask_siteid_sampling %in% mask_siteid_protocol]
trends_data <- abun_rich_op %>%
  left_join(op_protocol, by = "op_id") %>%
  filter(siteid %in% mask_siteid) %>%
  mutate(
    log_total_abundance = log(total_abundance),
    log_species_nb = log(species_nb)
  )
plot_community_data <- function(dataset = NULL, y = NULL, x = NULL, title = NULL) {

  p <- dataset %>%
    ggplot(aes_string(y = y, x = x)) +
    geom_point() +
    geom_smooth(method = "loess", formula = "y ~ x")

  if (!is.null(title)) {
    p <- p +
      labs(title = title)
  }

  return(p)
}

plot_trends <- trends_data %>%
  group_by(siteid) %>%
  nest() %>%
  ungroup() %>%
  slice_sample(n = 100) %>%
  mutate(
    p_abun = map2(data, siteid,
      ~plot_community_data(
        dataset = .x, y = "total_abundance", x = "year", title = .y)),
    p_rich = map2(data, siteid,
      ~plot_community_data(
        dataset = .x, y = "species_nb", x = "year", title = .y),
    )
  )

0.1 Total abundance

n_plot_by_batch <- 8
map(
  split(
    seq_len(nrow(plot_trends)),
    1:floor(nrow(plot_trends) / n_plot_by_batch) + 1),
  ~plot_grid(plotlist = plot_trends[.x, ]$p_abun)
  )
#> Warning in split.default(seq_len(nrow(plot_trends)), 1:floor(nrow(plot_trends)/
#> n_plot_by_batch) + : la taille de données n'est pas un multiple de la variable
#> découpée
#> Warning in simpleLoess(y, x, w, span, degree = degree, parametric =
#> parametric, : pseudoinverse used at 2007
#> Warning in simpleLoess(y, x, w, span, degree = degree, parametric =
#> parametric, : neighborhood radius 2
#> Warning in simpleLoess(y, x, w, span, degree = degree, parametric =
#> parametric, : reciprocal condition number 0
#> Warning in predLoess(object$y, object$x, newx = if
#> (is.null(newdata)) object$x else if (is.data.frame(newdata))
#> as.matrix(model.frame(delete.response(terms(object)), : pseudoinverse used at
#> 2007
#> Warning in predLoess(object$y, object$x, newx = if
#> (is.null(newdata)) object$x else if (is.data.frame(newdata))
#> as.matrix(model.frame(delete.response(terms(object)), : neighborhood radius 2
#> Warning in predLoess(object$y, object$x, newx = if
#> (is.null(newdata)) object$x else if (is.data.frame(newdata))
#> as.matrix(model.frame(delete.response(terms(object)), : reciprocal condition
#> number 0
#> $`2`

#> 
#> $`3`

#> 
#> $`4`

#> 
#> $`5`

#> 
#> $`6`

#> 
#> $`7`

#> 
#> $`8`

#> 
#> $`9`

#> 
#> $`10`

#> 
#> $`11`

#> 
#> $`12`

#> 
#> $`13`

0.2 Species richness


map(
  split(
    seq_len(nrow(plot_trends)),
    1:floor(nrow(plot_trends) / n_plot_by_batch) + 1
    ),
  ~plot_grid(plotlist = plot_trends[.x, ]$p_rich)
  )
#> Warning in split.default(seq_len(nrow(plot_trends)), 1:floor(nrow(plot_trends)/
#> n_plot_by_batch) + : la taille de données n'est pas un multiple de la variable
#> découpée
#> Warning in simpleLoess(y, x, w, span, degree = degree, parametric =
#> parametric, : pseudoinverse used at 2007
#> Warning in simpleLoess(y, x, w, span, degree = degree, parametric =
#> parametric, : neighborhood radius 2
#> Warning in simpleLoess(y, x, w, span, degree = degree, parametric =
#> parametric, : reciprocal condition number 0
#> Warning in predLoess(object$y, object$x, newx = if
#> (is.null(newdata)) object$x else if (is.data.frame(newdata))
#> as.matrix(model.frame(delete.response(terms(object)), : pseudoinverse used at
#> 2007
#> Warning in predLoess(object$y, object$x, newx = if
#> (is.null(newdata)) object$x else if (is.data.frame(newdata))
#> as.matrix(model.frame(delete.response(terms(object)), : neighborhood radius 2
#> Warning in predLoess(object$y, object$x, newx = if
#> (is.null(newdata)) object$x else if (is.data.frame(newdata))
#> as.matrix(model.frame(delete.response(terms(object)), : reciprocal condition
#> number 0
#> $`2`

#> 
#> $`3`

#> 
#> $`4`

#> 
#> $`5`

#> 
#> $`6`

#> 
#> $`7`

#> 
#> $`8`

#> 
#> $`9`

#> 
#> $`10`

#> 
#> $`11`

#> 
#> $`12`

#> 
#> $`13`

0.3 Analysis

0.4 Reproducibility

Reproducibility receipt

## datetime
Sys.time()
#> [1] "2021-12-07 10:36:23 CST"

## repository
if(requireNamespace('git2r', quietly = TRUE)) {
  git2r::repository()
} else {
  c(
    system2("git", args = c("log", "--name-status", "-1"), stdout = TRUE),
    system2("git", args = c("remote", "-v"), stdout = TRUE)
  )
}
#> Local:    main /home/alain/Documents/post-these/isu/RivFishTimeBiodiversityFacets
#> Head:     [45bbace] 2021-12-04: Add Rigal temporal analysis + finish the targets pipe

## session info
sessionInfo()
#> R version 4.0.5 (2021-03-31)
#> Platform: x86_64-pc-linux-gnu (64-bit)
#> Running under: Debian GNU/Linux 10 (buster)
#> 
#> Matrix products: default
#> BLAS:   /home/alain/.Renv/versions/4.0.5/lib/R/lib/libRblas.so
#> LAPACK: /home/alain/.Renv/versions/4.0.5/lib/R/lib/libRlapack.so
#> 
#> locale:
#>  [1] LC_CTYPE=fr_FR.UTF-8       LC_NUMERIC=C              
#>  [3] LC_TIME=fr_FR.UTF-8        LC_COLLATE=fr_FR.UTF-8    
#>  [5] LC_MONETARY=fr_FR.UTF-8    LC_MESSAGES=fr_FR.UTF-8   
#>  [7] LC_PAPER=fr_FR.UTF-8       LC_NAME=C                 
#>  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
#> [11] LC_MEASUREMENT=fr_FR.UTF-8 LC_IDENTIFICATION=C       
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#>  [1] cowplot_1.1.1           rnaturalearthdata_0.1.0 rnaturalearth_0.1.0    
#>  [4] mapview_2.10.0          sf_0.9-7                rmarkdown_2.11         
#>  [7] scales_1.1.1            kableExtra_1.3.1        here_1.0.1             
#> [10] magrittr_2.0.1          forcats_0.5.1           stringr_1.4.0          
#> [13] dplyr_1.0.4             purrr_0.3.4             readr_2.1.1            
#> [16] tidyr_1.1.2             tibble_3.1.6            ggplot2_3.3.3          
#> [19] tidyverse_1.3.0         tarchetypes_0.3.2       targets_0.8.1          
#> [22] conflicted_1.1.0        nvimcom_0.9-122        
#> 
#> loaded via a namespace (and not attached):
#>  [1] leafem_0.1.6       colorspace_2.0-0   ellipsis_0.3.2     class_7.3-18      
#>  [5] leaflet_2.0.4.1    rprojroot_2.0.2    satellite_1.0.4    base64enc_0.1-3   
#>  [9] fs_1.5.1           rstudioapi_0.13    farver_2.0.3       fansi_0.5.0       
#> [13] lubridate_1.7.9.2  xml2_1.3.2         codetools_0.2-18   splines_4.0.5     
#> [17] cachem_1.0.4       knitr_1.36         jsonlite_1.7.2     broom_0.7.4       
#> [21] dbplyr_2.1.0       png_0.1-7          compiler_4.0.5     httr_1.4.2        
#> [25] backports_1.2.1    assertthat_0.2.1   Matrix_1.3-2       fastmap_1.1.0     
#> [29] cli_3.1.0          htmltools_0.5.1.1  tools_4.0.5        igraph_1.2.6      
#> [33] gtable_0.3.0       glue_1.5.1         Rcpp_1.0.6         cellranger_1.1.0  
#> [37] jquerylib_0.1.3    raster_3.4-5       vctrs_0.3.8        nlme_3.1-152      
#> [41] crosstalk_1.1.1    xfun_0.28          ps_1.6.0           rvest_0.3.6       
#> [45] lifecycle_1.0.1    hms_1.1.1          yaml_2.2.1         memoise_2.0.0     
#> [49] sass_0.3.1         stringi_1.7.6      highr_0.9          e1071_1.7-4       
#> [53] rlang_0.4.12       pkgconfig_2.0.3    evaluate_0.14      lattice_0.20-41   
#> [57] htmlwidgets_1.5.3  labeling_0.4.2     processx_3.5.2     tidyselect_1.1.1  
#> [61] bookdown_0.24      R6_2.5.1           generics_0.1.0     DBI_1.1.1         
#> [65] pillar_1.6.4       haven_2.3.1        withr_2.4.3        mgcv_1.8-34       
#> [69] units_0.6-7        sp_1.4-5           modelr_0.1.8       crayon_1.4.2      
#> [73] KernSmooth_2.23-18 utf8_1.2.2         tzdb_0.2.0         grid_4.0.5        
#> [77] readxl_1.3.1       data.table_1.13.6  callr_3.7.0        git2r_0.29.0      
#> [81] reprex_1.0.0       digest_0.6.27      classInt_0.4-3     webshot_0.5.2     
#> [85] stats4_4.0.5       munsell_0.5.0      viridisLite_0.3.0  bslib_0.2.4